/**
 * 通过元素 ID 获取 DOM 元素
 * 原函数名: $
 * @param {string} id - 元素的 ID
 * @returns {HTMLElement} - 获取到的 DOM 元素
 */
function $(t) {
  return document.getElementById(t)
}
/**
 * 发送 HTTP 请求
 * 原函数名: o
 * @param {string} url - 请求的 URL
 * @param {string|null} data - 请求的数据，若为 null 则发送 GET 请求
 * @param {function} callback - 请求成功后的回调函数
 */
function o(t, e, n) {
  var o = new XMLHttpRequest;
  o.open(null != e ? "POST" : "GET", t, !0),
  o.onreadystatechange = function() {
    4 != o.readyState || 200 != o.status && 304 != o.status || n(o.responseText)
  }
  ,
  o.send(e)
}
var Z; //1 isFirstClick
var I; //2 leftMouseButtonSetting
var y; //3 rightMouseButtonSetting
var S; //4 lastClickX
var w; //5 oneClickOpenSetting
var tt; //6 touchTimer
var et = []; //7 touchTimers
var nt = 0; //8 touchTimerCount
var n; //9 startTime
var t; //10 currentTime
var T; //11 touchButtonSetting
var k; //12 oneClickOpenSetting
var M; //13 isMobile
var x; //14 remainingMines_byUser
var _; //15 uncoverBlocks
var C; //16 cavas
var B; //17 context
var D; //18 updateRemainingMines
var R; //19 updateElapsedTime
var E = 0; //20 gameTimer
var m; //21 boardWidth
var v; //22 boardHeight
var r; //23 mineCount
var l; //24 gameState
var a; //25 emptyCells
var d = []; //26 cellStates
var h = [-1, -1, -1, 0, 0, 1, 1, 1, 0]; //27 horizontalOffsets
var p = [-1, 0, 1, -1, 1, -1, 0, 1, 0]; //28 verticalOffsets
var e; //29 gameDifficultyLevel
var X; //30 totalCells
var L; //31 screenWidth
var gt; //32 screenHeight
//var mt; //33 baseUrl
var vt = null; //34 userNickname
var b; //35 autoFlagging
var ct = 0; //36 clickCount
var s = []; //37 cellIndices
var g = []; //38 cellMapping
/**
 * 把格子恢复成未揭秘时的方块样子
 * revealCell
 * @param {number} x - 格子的横坐标
 * @param {number} y - 格子的纵坐标
 */
// done
function N(t, e) {
  B.drawImage(gfb[0], 25 * t, 25 * e),
  setTimeout(function() {
    0 == d[e][t][0] && B.drawImage(gfs[0], 25 * t, 25 * e)
  }, 120)
}
/**
 * 检查周围格子
 * checkSurroundingCells
 * @param {number} x - 格子的横坐标
 * @param {number} y - 格子的纵坐标
 */
function c(t, e) {
  for (var n, o = 0, r = 0, a = 0; a < 8; a++)
    u = e + p[a],
    i = t + h[a],
    0 <= u && u < v && 0 <= i && i < m && (2 == (n = (c = d[u][i])[0]) ? o++ : 0 == n && 1 == c[1] && (r = 1));
  var l = o >= d[e][t][2];
  for (a = 0; a < 8; a++) {
    var c, u = e + p[a], i = t + h[a];
    0 <= u && u < v && 0 <= i && i < m && 0 == (c = d[u][i])[0] && (l ? r ? 1 == c[1] && (B.drawImage(gfs[2], 25 * i, 25 * u),
    c[0] = 1) : f(i, u) : N(i, u))
  }
  l && r && V()
}
/**
 * 游戏结束处理
 * gameOver
 */
function V() {
  var t, e, n;
  for (ft(),
  $("face").src = gif[2],
  l = 3,
  e = 0; e < v; e++)
    for (t = 0; t < m; t++)
      0 == (n = d[e][t])[0] ? 1 == n[1] && B.drawImage(gfs[3], 25 * t, 25 * e) : 2 == n[0] && 0 == n[1] && (et[nt++] = setInterval(function(t, e) {
        var n = 0;
        return function() {
          B.drawImage(0 == n ? gfb[d[e][t][2]] : gfs[1], 25 * t, 25 * e),
          n = !n
        }
      }(t, e), 800))
}

/**
 * 检查游戏状态
 * checkGameStatus
 * @returns {number} - 游戏状态码
 */
function q() {
  a = [];
  for (var t, e, n = 0, o = 0, r = 0; r < v; r++)
    for (t = 0; t < m; t++) {
      if (2 == (e = d[r][t])[0] && 1 != e[1])
        return 1;
      0 == e[0] && 0 == e[3] && (1 == e[1] ? n++ : o++,
      a.push([t, r]))
    }
  return n != o ? 2 : 0
}
function G(t, e) {
  for (var n, o, r = 0, a = 0; a < 8; a++)
    o = e + p[a],
    n = t + h[a],
    0 <= o && o < v && 0 <= n && n < m && 1 == d[o][n][1] && r++;
  return r
}
/**
 * 切换标记状态
 * 原函数名: toggleFlags
 */
function O() {
  for (var t, e = 0; e < a.length; e++)
    t = a[e],
    (t = d[t[1]][t[0]])[1] = 0 == t[1] ? 1 : 0
}
function P() {
  if (0 != q())
    return 1;
  var t, e, n, o;
  for (O(),
  e = 0; e < v; e++)
    for (t = 0; t < m; t++)
      if (o = (n = d[e][t])[2],
      1 == n[0] && 0 != o && G(t, e) != o)
        return O(),
        2;
  for (e = 0; e < v; e++)
    for (t = 0; t < m; t++)
      1 != (n = d[e][t])[0] && (n[2] = G(t, e));
  return 0
}
function W(t, e) {
  for (var n, o, r, a, l, c, u, i, f, s, g = 0; g < 9; g++)
    if (f = e + p[g],
    i = t + h[g],
    0 <= f && f < v && 0 <= i && i < m && (u = (n = d[f][i])[2],
    1 == n[0]) && 0 < u) {
      for (s = c = 0; s < 8; s++)
        r = f + p[s],
        a = i + h[s],
        0 <= r && r < v && 0 <= a && a < m && (0 == (l = (o = d[r][a])[0]) ? c++ : 2 == l && (1 == o[1] ? u-- : c++));
      if (0 < c && u == c)
        for (s = 0; s < 8; s++)
          r = f + p[s],
          a = i + h[s],
          0 <= r && r < v && 0 <= a && a < m && (1 != (l = (o = d[r][a])[0]) && (o[3] = 1),
          0 == l) && 1 == b && H(a, r)
    }
}
function f(t, e) {
  var n = d[e][t];
  if (1 == n[1] && (1 == l && P(),  1 == n[1]))
    return B.drawImage(gfs[2], 25 * t, 25 * e), n[0] = 1, V(), 1;
  if (n[0] = 1, B.drawImage(gfb[n[2]], 25 * t, 25 * e), 0 == --_)
    j(); // winGame();
  else if (0 == n[2])
    for (var o, r, a = 0; a < 8; a++)
      o = e + p[a], r = t + h[a], 0 <= o && o < v && 0 <= r && r < m && 0 == d[o][r][0] && f(r, o);
  return W(t, e), 0
}
function j() {
  var t, e, n;
  for (l = 2, //gameState = 2;
  ft(), //stopGameTimer();
  e = 0; e < v; e++)
    for (t = 0; t < m; t++)
      0 == (n = d[e][t])[0] && (1 != n[1] ? It(1, t, e) : H(t, e));
  0 != x && It(2, t, e),
  D(r),
  $("face").src = gif[1];
}
function J(t) {
  var o = $(t)
    , r = o.getContext("2d")
    , a = 3;
  return function(t) {
    var e = (t = t < 10 ? "00" + t : t < 100 ? "0" + t : t.toString()).length;
    e != a && (o.width = 13 * e,
    a = e);
    for (var n = 0; n < e; n++)
      r.drawImage(gfd[parseInt(t.charAt(n))], 13 * n, 0)
  }
}
function u(t, e) {
  for (var n = _, o = 8; 0 <= o && 0 < n; o--) {
    var r, a = e + p[o], l = t + h[o];
    0 <= a && a < v && 0 <= l && l < m && (r = g[a * m + l],
    1 == d[a][l][1] ? (a = Math.floor(Math.random() * n),
    U(r, -1),
    U(a, 1),
    Q(a, --n)) : Q(r, --n))
  }
  st()
}
function K() {
  for (var t, e, n, o = 0; o < v; o++)
    for (d[o] = [],
    t = 0; t < m; t++)
      d[o][t] = [0, 0, 0, 0];
  for (n = 0; n < X; n++)
    s[n] = n,
    g[n] = n;
  for (_ = X,
  e = 0; e < r; e++)
    Q(n = Math.floor(Math.random() * _), --_);
  for (n = _; n < X; n++)
    U(n, 1);
  _ = X - (x = r)
}
function Q(t, e) {
  var n = s[t]
    , o = s[e];
  s[t] = o,
  s[e] = n,
  g[n] = e,
  g[o] = t
}
function U(t, e) {
  var t = s[t]
    , n = Math.floor(t / m)
    , o = t % m;
  for (d[n][o][1] += e,
  z = 0; z < 8; z++)
    cy = n + p[z],
    cx = o + h[z],
    0 <= cy && cy < v && 0 <= cx && cx < m && (d[cy][cx][2] += e)
}
function _45() {
  0 < E && (clearInterval(E),
  E = 0);
  for (var t = 0; t < nt; t++)
    clearInterval(et[t]);
  nt = 0,
  K(),
  it(),
  w = S = -1,
  Z = 1,
  l = 0
}
function ot(t) {
  var e, n, o;
  M
  || 1 < l
  || (n = C.getBoundingClientRect(),
      e = Math.floor((t.clientX - n.left) / 25),
      n = Math.floor((t.clientY - n.top) / 25),
      e < 0)
  || e == m
  || n < 0
  || n == v
  || (S = e,
      o = d[w = n][e][0],
      2 == t.button ?
             1 != y && (1 == o ? c : H)(e, n) :
             1 != I && (0 == o ?
                          (0 == l && u(e, n), f) :
                          1 == o ? c : H)(e, n))
}
function rt(t) {
  var e, n, o;
  M || 1 < l || (n = C.getBoundingClientRect(),
  e = Math.floor((t.clientX - n.left) / 25),
  n = Math.floor((t.clientY - n.top) / 25),
  e < 0) || e == m || n < 0 || n == v || (o = d[n][e][0],
  2 == t.button ? null != y && (1 == o ? c : H)(e, n) : null != I && (0 == o ? (0 == l && u(e, n),
  f) : 1 == o ? c : H)(e, n))
}
function at(t) {
  var e, n, o;
  1 < l || (M = 1,
  e = C.getBoundingClientRect(),
  n = Math.floor((t.touches[0].clientX - e.left) / 25),
  o = Math.floor((t.touches[0].clientY - e.top) / 25),
  n < 0) || n == m || o < 0 || o == v || (1 == d[o][n][0] ? c(n, o) : tt = setTimeout(function() {
    return lt(n, o)
  }, 320))
}
function lt(t, e) {
  1 == k && null == T || (0 == l ? (u(t, e),
  f(t, e)) : (M = 3,
  null == T ? (2 == d[e][t][0] && H(t, e),
  0 == d[e][t][0] && f(t, e)) : H(t, e)))
}
function ut(t) {
  if (1 == M) {
    var e = C.getBoundingClientRect()
      , n = Math.floor((t.changedTouches[0].clientX - e.left) / 25)
      , e = Math.floor((t.changedTouches[0].clientY - e.top) / 25);
    if (n < 0 || n == m || e < 0 || e == v)
      return;
    var o, r, a = d[e][n][0];
    0 == l ? (u(n, e),
    f(n, e)) : null == T ? 1 != a && (o = Date.now(),
    r = n == S && e == w ? o - ct : (S = n,
    w = e,
    ct = Date.now(),
    1e3),
    (1 == k && r < 400 && 0 == a ? f : H)(n, e),
    ct = o) : 0 == a ? f(n, e) : 2 == a && H(n, e),
    M = 4,
    clearTimeout(tt)
  }
  t.preventDefault ? t.preventDefault() : window.event.returnValue = !1
}
function saf() {
  b = $("af").checked ? 1 : 0,
  localStorage.setItem("af", b),
  _45()
}
function it() {
  ft(),
  M = E = 0,
  I = localStorage.getItem("_mp1"),
  y = localStorage.getItem("_mp2"),
  T = localStorage.getItem("tpn"),
  k = localStorage.getItem("opn"),
  null == (b = localStorage.getItem("af")) && (b = 0);
  var t = 25 * m;
  $("p42").style.width = 4 + t + "px",
  C.width = t,
  C.height = 25 * v,
  $("face").src = gif[0];
  for (var e = 0; e < m; e++)
    for (var n = 0; n < v; n++)
      B.drawImage(gfs[0], 25 * e, 25 * n);
  C.onmousedown = function(t) {
    ot(t)
  },
  C.onmouseup = function(t) {
    rt(t)
  },
  C.ontouchstart = function(t) {
    at(t)
  },
  C.ontouchmove = function() {
    M = 2,
    clearTimeout(tt)
  },
  C.ontouchend = function(t) {
    ut(t)
  },
  D(x),
  R(0),
  $("af").checked = parseInt(b)
}
function ft() {
  n = 0 < E ? (clearInterval(E),
  E = 0,
  n = Date.now() - n,
  R(parseInt(n / 1e3)),
  Math.ceil(n / 100)) : 0
}
function st() {
  n = Date.now(),
  t = 0,
  l = 1,
  E = setInterval(function() {
    R(++t)
  }, 1e3)
}
function H(t, e) {
  Z = 0; // isFirstClick = false;
  var n = d[e][t];
  0 == n[0] ? 0 < x && (0 == n[1] && P(),
  B.drawImage(gfs[1], 25 * t, 25 * e),
  n[0] = 2,
  D(--x)) : 2 == n[0] && (B.drawImage(gfs[0], 25 * t, 25 * e),
  n[0] = 0,
  D(++x))
}

function SetupDebugCavas() {
  for (let i=0; i<4; i++) {
    const canvas = $("gameCanvas"+i);
    canvas.width = m * 25;
    canvas.height = v * 25;
  }
}
function DrawDebugCanvas() {
  for (let i=0; i<4; i++) {
    const canvas = $("gameCanvas"+i);
    const ctx = canvas.getContext("2d");
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.font = '30px Arial'; // 设置字体大小和类型
    ctx.fillStyle = '#FF0000'; // 设置填充颜色为红色
    ctx.strokeStyle = '#FF0000'; // 设置描边颜色为红色
    //ctx.lineWidth = 2; // 设置线宽为2
    for (let y = 0; y < v; y++) {
      for (let x = 0; x < m; x++) {
        const d1 = d[y][x][i];
        ctx.fillText(d1, x*25, (y+1)*25);
      }
    }
  }
}
function start() {
  //mt = $("ss").href + "#";
  var t = localStorage.getItem("df5");
  null == t ? ($("hm").value = 15,
  $("vm").value = 15,
  $("mm").value = 20) : (t = t.split(";"),
  $("hm").value = t[0],
  $("vm").value = t[1],
  $("mm").value = t[2]),
  document.oncontextmenu = function() {
    return !1
  }
  ,
  document.onselectstart = function() {
    return !1
  }
  ,
  C = $("paf"),
  B = C.getContext("2d"),
  D = J("rm"),
  R = J("es"),
  _123(localStorage.getItem("ch7")),
  $("uid").innerHTML = dt()
}
function dt() {
  var t;
  return null != vt ? vt : null != (t = localStorage.getItem("nick")) && "" != t ? t : localStorage.getItem("uid")
}
function ht() {
  o("uid2.php", null, function(t) {
    1 < t.length ? (vt = t,
    F(),
    $("uid").innerHTML = t,
    localStorage.setItem("uid", t)) : alert("id fail")
  })
}
function _123(t) {
  if (l = 0,
  L = document.body.clientWidth,
  gt = document.body.clientHeight,
  e = t = null == t ? L < 560 ? 1 : 2 : t,
  $("custom").style.display = 5 == t ? "" : "none",
  1 == t)
    v = m = 9,
    r = 10,
    X = 81;
  else if (2 == t)
    v = m = 16,
    r = 40,
    X = 256;
  else if (3 == t)
    r = 99,
    v = gt < L ? (m = 30,
    16) : (m = 16,
    30),
    X = 480;
  else if (4 == t)
    m = parseInt((L - 18) / 25),
    v = parseInt((gt - 54) / 25),
    r = 480 <= (X = m * v) ? .20625 * X : X * X / 5760 + X / 8,
    r = parseInt(r);
  else {
    if (5 != t)
      return;
    m = parseInt($("hm").value),
    v = parseInt($("vm").value),
    X = m * v,
    r = parseInt($("mm").value),
    X < r && (r = X)
  }
  _45(),
  localStorage.setItem("ch7", t),
  //$("ss").href = mt + e
  SetupDebugCavas();
}
function udf() {
  _123(5),
  localStorage.setItem("df5", $("hm").value + ";" + $("vm").value + ";" + $("mm").value)
}
function Y(t) {
  var e = localStorage.getItem(t)
    , n = (null == e && (e = 0),
  document.getElementsByName(t));
  for (i = 0; i < n.length; i++)
    n[i].value == e && (n[i].checked = !0);
  return e
}
var pt = ["_mp1", "_mp2", "tpn", "opn"];
function _save(t, e) {
  var n = pt[t];
  0 == e ? (localStorage.removeItem(n),
  e = null) : localStorage.setItem(n, e),
  0 == t ? I = e : 1 == t ? y = e : 2 == t ? T = e : k = e
}
function smore(t) {
  0 == t ? ($("topen").style.display = "block",
  $("thint").style.display = "none") : ($("thint").style.display = "block",
  $("topen").style.display = "none")
}
var mouse2 = function() {
  var t = 0;
  return function() {
    Y("_mp1"),
    Y("_mp2"),
    t = 0 == t ? ($("_mouse").style.display = "block",
    $("setm").innerText = shou,
    1) : ($("_mouse").style.display = "none",
    $("setm").innerText = shez,
    0)
  }
}()
  , touch2 = function() {
  var e = 0;
  return function() {
    var t = Y("tpn");
    Y("opn"),
    smore(t),
    e = 0 == e ? ($("_touch").style.display = "block",
    $("sett").innerText = shou,
    1) : ($("_touch").style.display = "none",
    $("sett").innerText = shez,
    0)
  }
}()
  , setr = function() {
  var t = 0;
  return function() {
    t = 0 == t ? ($("nick").value = localStorage.getItem("nick"),
    $("_rename").style.display = "block",
    $("setr").innerText = shou,
    1) : ($("_rename").style.display = "none",
    $("setr").innerText = shez,
    0)
  }
}();
function rename() {
  var t = $("nick").value.trim();
  t.charCodeAt(0) < 65 ? alert(w1) : (localStorage.setItem("nick", t),
  $("uid").innerHTML = dt(),
  setr())
}
function bcolor() {
  var t = document.body.style
    , e = document.getElementsByTagName("a");
  if ($("bcolor").checked) {
    localStorage.setItem("night", "1"),
    t.backgroundColor = "black",
    t.color = "silver";
    for (var n = 0; n < e.length; n++)
      e[n].style.color = "silver"
  } else {
    localStorage.removeItem("night"),
    t.backgroundColor = "#f7f7f0",
    t.color = "";
    for (n = 0; n < e.length; n++)
      e[n].style.color = ""
  }
}
function night() {
  null != localStorage.getItem("night") && ($("bcolor").checked = !0,
  bcolor())
}
function It(t, e, n) {
  o("bug.php", VER + ":" + t, function(t) {})
}
